package graph.impl;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class MyGraph<E extends Comparable<E>> {

	private Map<E,Set<E>> graph = null;
	protected int vertexCount = 0; 
	
	public MyGraph(){
		graph = new HashMap<E,Set<E>>();
	}
	
	// adds new vertex to the graph
	public void addVertex(E e){
		if(this.graph.containsKey(e)){
			return;
		}
		this.graph.put(e, new HashSet<E>());
		vertexCount++;
	}

	// adding edges to the vertex. if vertex not available creates a new one 
	
	public void addEdgeTo(E v1, E v2){
		
		Set<E> edgeSet = this.graph.get(v1);		
		if(	edgeSet !=null	){
			edgeSet.add(v2);
		}	else	{
			addVertex(v1);
			addEdgeTo(v1,v2);
		}
	}
	
	public Set<E> getEdges(String sourceVertex){
		return this.graph.get(sourceVertex);
	}
	
	public Set<E> getVertices(){
		return this.graph.keySet();
	}
	// prints the graph Datastructure;
	
	public String toString(){
		StringBuilder graphAsString = new StringBuilder();
		String 	NEWLINE = "\n";
		
		for(E v : this.graph.keySet()){
			graphAsString.append(v.toString() + " --> ");
			for(E e : this.graph.get(v)){
				graphAsString.append(e.toString() + ",");
			}
			graphAsString.append(NEWLINE);
		}
		return graphAsString.toString();
	}
	
}
